//
//  DetailShowImageView.m
//  花花时尚
//
//  Created by 霍文轩 on 15/8/26.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//

#import "DetailShowImageView.h"

@interface DetailShowImageView()
@property (nonatomic,retain) UILabel * label;
@end

@implementation DetailShowImageView

-(instancetype)initWithFrame:(CGRect)frame text:(NSString *)text{
    self =  [super initWithFrame:frame];
    if(self){
        self.userInteractionEnabled = YES;
        
        if( ! [text isEqualToString:@""])  // 如果text不为空就创建label
            [self createLabelWithText:text];
    }
    return self;
}
-(void)createLabelWithText:(NSString *)text{
    CGFloat labelHeight = [self heightWithText:text];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height-labelHeight, self.frame.size.width, labelHeight)];
    [self addSubview:_label];
    _label.backgroundColor = [UIColor colorWithRed:105/255.0 green:106/255.0 blue:107/255.0 alpha:0.5];
    _label.textColor = [UIColor whiteColor];
    _label.text = text;
    _label.numberOfLines = 0;
    _label.font = [UIFont systemFontOfSize:17];
    _label.textAlignment = NSTextAlignmentCenter;
    [_label release];
}
#pragma mark -  根据文本设定大小
- (CGFloat)heightWithText:(NSString *)text{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(self.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil]; // 第一个参数：预定的矩形大小。第二个：图形选项。第三个：属性字典，设置字体和字体大小，一般和cell中设置的一样。第四个：内容文本，一般为空
    return rect.size.height;
}
#pragma  mark - 进入图片下载页面
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.delegate enterImageWithImage:self.image imgRect:self.frame];
}
@end
